package com.ldzy.controller;

import com.ldzy.pojo.User;
import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.xml.ws.spi.http.HttpHandler;
import java.io.File;
import java.io.IOException;

@Controller
public class MyFirstController {
    @RequestMapping("/toMain")
    public String toMainPage(){
        return "main";
    }

    @RequestMapping("/toLogin")
    public String toLoginPage(){
        return "login";
    }

    @RequestMapping("/toOrderinfo")
    public String toOrderinfoPage(){
        return "orderinfo";
    }
    @RequestMapping("/tofileload")
    public  String tofileload(){
        return "fileload";
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)//从登录页面来的请求
    public ModelAndView login(User user, HttpSession session){
        ModelAndView modelAndView=new ModelAndView();
        if (user.getName().equals("Tom")&&user.getPassword().equals("123")){
            modelAndView.setViewName("main");
            session.setAttribute("USER_SESSION",user);
        }else {
            modelAndView.setViewName("login");
            modelAndView.addObject("msg","用户名或密码不正确！");
        }
        return modelAndView;
    }

    @RequestMapping("/logout")
    public ModelAndView logout(HttpSession session){
        ModelAndView modelAndView=new ModelAndView();
        session.removeAttribute("USER_SESSION");
        modelAndView.setViewName("login");
        modelAndView.addObject("msg","您还没有登录，请登录！");
        return modelAndView;
    }
    @RequestMapping("/toupload")
    public String toupload(){
        System.out.println("get upload");
        return "upload";
    }
    @RequestMapping("/upload")
    public String upload( MultipartFile file, HttpServletRequest request)throws IOException{
        String path=request.getServletContext().getRealPath("/")+"files"+File.separator+file.getOriginalFilename();
        System.out.println(path);
//        XYChart.Data data =new
        if (!file.isEmpty()){
        file.transferTo(new File(path));
            System.out.println("uploadSuccess");
        return "uploadSuccess";

        }
        System.out.println("uploadFailure");
        return "uploadFailure";
    }
    @RequestMapping("downlesd")
    public ResponseEntity download(String filename, HttpServletRequest request) throws IOException {
        String path=request.getServletContext().getRealPath("/")+"files"+File.separator+filename;
        File file=new File(path);
        HttpHeaders httpHeaders=new HttpHeaders();
        httpHeaders.setContentDispositionFormData("attachment",filename);
        httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return  new ResponseEntity<byte []>(FileUtils.readFileToByteArray(file),httpHeaders, HttpStatus.OK);
    }
}
